﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IService;
using IRepository;
using Repository;
using Entity;
using System.Data;

namespace Service
{
    public class SupplierManagementService : ISupplierManagementService
    {
        private readonly ISupplierManagementRepository _repository;

        // 注入仓储接口（依赖注入）
        public SupplierManagementService(ISupplierManagementRepository repository)
        {
            _repository = repository;
        }

        public async Task AddAsync(WMSBasicSupplierManagement supplier)
        {
            // 业务校验（示例：检查供应商名称是否为空）
            if (string.IsNullOrWhiteSpace(supplier.supplier_name))
            {
                throw new ArgumentException("供应商名称不能为空");
            }

            // 调用仓储层执行入库
            await _repository.AddAsync(supplier);
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="supplier_code"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task Delete(string supplier_code)
        {
            if (string.IsNullOrEmpty(supplier_code))
                throw new ArgumentNullException("删除失败，请重试");
            await _repository.Delete(supplier_code);
        }

        public async Task<PagedViewModel<SupplierListDTO>> GetAllSuppliersAsync(
            string supplierName = null,
            string status = null,
            int? industryId = null,
            string managerName = null,
            int currentPage = 1,
            int pageSize = 10)
        {
            // 1. 查询符合条件的全量数据
            List<SupplierListDTO> allSuppliers = await _repository.GetAllSuppliersAsync(
                supplierName, status, industryId, managerName);

            // 2. 内存分页（Skip + Take）
            List<SupplierListDTO> paginatedSuppliers = allSuppliers
                .Skip((currentPage - 1) * pageSize)  // 跳过前N条
                .Take(pageSize)                       // 取当前页数据
                .ToList();

            // 3. 封装分页模型（包含“当前页数据”和“总条数”）
            PagedViewModel<SupplierListDTO> result = new PagedViewModel<SupplierListDTO>
            {
                items = paginatedSuppliers,  // 修正：与PagedViewModel的属性名保持一致（建议用items）
                total = allSuppliers.Count    // 总数据条数
            };

            return result; // 现在返回值与Task<PagedViewModel<SupplierListDTO>>匹配
        }

        /// <summary>
        /// 为下拉框提供数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public Task<List<SupplierListDTO>> GetAllSuppliersAsync()
        {
            try
            {
                return _repository.GetAllSuppliersAsync();
            }
            catch (Exception)
            {
                throw new ArgumentNullException("连接超时");
            }
        }

        /// <summary>
        /// 根据供应商编号获取详情
        /// </summary>
        public async Task<WMSBasicSupplierManagement> GetSupplierByCodeAsync(string supplierCode)
        {
            if (string.IsNullOrEmpty(supplierCode))
                throw new ArgumentNullException("供应商编号不能为空");

            return await _repository.GetSupplierByCodeAsync(supplierCode);
        }

        public async Task<WMSBasicSupplierManagement> GetSupplierByIdAsync(long supplierId)
        {
            return await _repository.GetSupplierByIdAsync(supplierId);
        }

        public async Task UpdateAsync(WMSBasicSupplierManagement supplier)
        {
            // 业务校验（示例：检查供应商名称是否为空）
            if (string.IsNullOrWhiteSpace(supplier.supplier_name))
            {
                throw new ArgumentException("供应商名称不能为空");
            }

            // 调用仓储层执行更新
            await _repository.UpdateAsync(supplier);
        }
    }
}
